import 'dart:convert';

/// 订单详情 model
class OrderDetailModel {
  int code;
  String msg;
  DetailInfoModel? data;

  OrderDetailModel({
    required this.code,
    required this.msg,
    this.data,
  });

  // 从 JSON 创建 OrderDetailModel
  factory OrderDetailModel.fromJson(Map<String, dynamic> json) {
    return OrderDetailModel(
      code: json['code'] ?? 0,
      msg: json['msg'] ?? '',
      data: json['data'] != null
          ? DetailInfoModel.fromJson(json['data'])
          : null,
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'msg': msg,
      'data': data?.toJson(),
    };
  }
}

/// 订单详情中的详细信息
class DetailInfoModel {
  String attrs;
  String attrValue;
  String cancelTime;
  String payTime;
  String cardsInfo;
  String closeType;
  String country;
  String couponsPrice;
  String createTime;
  String fee;
  String orderId;
  String payName;
  String pic;
  String priceFee;
  String spuName;
  String subtotalMoney;
  String symbol;
  String paymentAmount;
  int? rechargeType;
  double commentState;
  double num;
  double payBalance;
  double payType;
  double refundState;
  double shopType;
  int spuId;
  double status;
  DetailActivity? activity;

  DetailInfoModel({
    required this.attrs,
    required this.attrValue,
    required this.cancelTime,
    required this.payTime,
    required this.cardsInfo,
    required this.closeType,
    required this.country,
    required this.couponsPrice,
    required this.createTime,
    required this.fee,
    required this.orderId,
    required this.payName,
    required this.pic,
    required this.priceFee,
    required this.spuName,
    required this.subtotalMoney,
    required this.symbol,
    required this.paymentAmount,
    required this.commentState,
    required this.num,
    required this.payBalance,
    required this.payType,
    required this.refundState,
    required this.shopType,
    required this.spuId,
    required this.status,
    required this.activity,
    required this.rechargeType,
  });

  // 从 JSON 创建 DetailInfoModel
  factory DetailInfoModel.fromJson(Map<String, dynamic> json) {
    return DetailInfoModel(
      attrs: json['attrs'] ?? '',
      attrValue: json['attrvalue'] ?? '',
      cancelTime: json['cancelTime'] ?? '',
      payTime: json['payTime'] ?? '',
      cardsInfo: json['cardsInfo'] ?? '',
      closeType: json['closeType'] ?? '',
      country: json['country'] ?? '',
      couponsPrice: json['couponsPrice'] ?? '',
      createTime: json['createTime'] ?? '',
      fee: json['fee'] ?? '',
      orderId: json['orderId'] ?? '',
      payName: json['payName'] ?? '',
      pic: json['pic'] ?? '',
      priceFee: json['priceFee'] ?? '',
      spuName: json['spuName'] ?? '',
      subtotalMoney: json['subtotalMoney'] ?? '',
      symbol: json['symbol'] ?? '',
      paymentAmount: json['paymentAmount'] ?? '',
      commentState: (json['commentState'] ?? 0).toDouble(),
      num: (json['num'] ?? 0).toDouble(),
      payBalance: (json['payBalance'] ?? 0).toDouble(),
      payType: (json['payType'] ?? 0).toDouble(),
      refundState: (json['refundState'] ?? 0).toDouble(),
      shopType: (json['shopType'] ?? 0).toDouble(),
      spuId: json['spuId'] ?? 0,
      status: (json['status'] ?? 0).toDouble(),
      rechargeType: json['rechargeType'] ?? 0,
      activity: json['activity'] != null
          ? DetailActivity.fromJson(json['activity'])
          : null,
    );
  }

  // 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'attrs': attrs,
      'attrvalue': attrValue,
      'cancelTime': cancelTime,
      'payTime': payTime,
      'cardsInfo': cardsInfo,
      'closeType': closeType,
      'country': country,
      'couponsPrice': couponsPrice,
      'createTime': createTime,
      'fee': fee,
      'orderId': orderId,
      'payName': payName,
      'pic': pic,
      'priceFee': priceFee,
      'spuName': spuName,
      'subtotalMoney': subtotalMoney,
      'symbol': symbol,
      'paymentAmount': paymentAmount,
      'commentState': commentState,
      'num': num,
      'payBalance': payBalance,
      'payType': payType,
      'refundState': refundState,
      'shopType': shopType,
      'spuId': spuId,
      'status': status,
      'rechargeType': rechargeType,
      'activity': activity,
    };
  }
}

class DetailActivity {
  double cill = 0;
  String name = '';
  String effectiveTime = '';
  String infor = '';
  String createTime = '';
  double parvalue = 0;
  String description = '';
  int spuCouponsId = 0;
  String scope = '';
  String symbol = '';
  String type = '';
  int status = 0;

  DetailActivity();

  factory DetailActivity.fromJson(Map<String, dynamic> json) {
    return DetailActivity()
      ..cill = (json['cill'] ?? 0).toDouble()
      ..name = json['name'] ?? ''
      ..effectiveTime = json['effectiveTime'] ?? ''
      ..infor = json['infor'] ?? ''
      ..createTime = json['createTime'] ?? ''
      ..parvalue = (json['parvalue'] ?? 0).toDouble()
      ..description = json['description'] ?? ''
      ..spuCouponsId = json['spuCouponsId'] ?? 0
      ..scope = json['scope'] ?? ''
      ..symbol = json['symbol'] ?? ''
      ..type = json['type'] ?? ''
      ..status = json['status'] ?? 0;
  }
}


class OrderModel {
  int code = 0;
  String msg = "";
  OrderInfoModel? data;

  OrderModel();

  OrderModel.fromJson(Map<String, dynamic> json) {
    code = json['code'] ?? 0;
    msg = json['msg'] ?? "";
    data = json['data'] != null ? OrderInfoModel.fromJson(json['data']) : null;
  }
}

class OrderInfoModel {
  String orderId = "";
  String redirectUrl = "";
  OrderInfoModel();

  OrderInfoModel.fromJson(Map<String, dynamic> json) {
    orderId = json['orderId'] ?? "";
    redirectUrl = json['redirectUrl'] ?? "";
  }
}

class UseepayData {
  final int code;
  final String msg;
  final UseepayModel data;

  UseepayData({
    required this.code,
    required this.msg,
    required this.data,
  });

  factory UseepayData.fromJson(Map<String, dynamic> json) {
    return UseepayData(
      code: json['code'] ?? 0,
      msg: json['msg'] ?? '',
      data: UseepayModel.fromJson(json['data'] ?? {}),
    );
  }
}

class UseepayModel {
  final String? surname;
  final String? givenName;
  final String? email;

  UseepayModel({
    this.surname,
    this.givenName,
    this.email,
  });

  factory UseepayModel.fromJson(Map<String, dynamic> json) {
    return UseepayModel(
      surname: json['surname'],
      givenName: json['givenName'],
      email: json['email'],
    );
  }
}

class OrderThreeModel {
  int code;
  String msg;
  OrderInfoThreeModel? data;

  OrderThreeModel({
    required this.code,
    required this.msg,
    this.data,
  });

  factory OrderThreeModel.fromJson(Map<String, dynamic> json) {
    return OrderThreeModel(
      code: json['code'] ?? 0,
      msg: json['msg'] ?? '',
      data: json['data'] != null ? OrderInfoThreeModel.fromJson(json['data']) : null,
    );
  }
}

class OrderInfoThreeModel {
  String redirectUrl;
  String orderId;
  OrderInfoPayModel? payInfo;

  OrderInfoThreeModel({
    required this.redirectUrl,
    required this.orderId,
    this.payInfo,
  });

  factory OrderInfoThreeModel.fromJson(Map<String, dynamic> json) {
    return OrderInfoThreeModel(
      redirectUrl: json['redirectUrl'] ?? '',
      orderId: json['orderId'] ?? '',
      payInfo: json['payInfo'] != null ? OrderInfoPayModel.fromJson(json['payInfo']) : null,
    );
  }
}

class OrderInfoPayModel {
  String id;
  String clientSecret;
  String currency;

  OrderInfoPayModel({
    required this.id,
    required this.clientSecret,
    required this.currency,
  });

  factory OrderInfoPayModel.fromJson(Map<String, dynamic> json) {
    return OrderInfoPayModel(
      id: json['id'] ?? '',
      clientSecret: json['client_secret'] ?? '',
      currency: json['currency'] ?? '',
    );
  }
}

class OrderHomeModel {
  int code;
  String msg;
  OrderHomeDetailModel? data;

  OrderHomeModel({
    required this.code,
    required this.msg,
    this.data,
  });

  factory OrderHomeModel.fromJson(Map<String, dynamic> json) {
    return OrderHomeModel(
      code: json['code'] ?? 0,
      msg: json['msg'] ?? '',
      data: json['data'] != null ? OrderHomeDetailModel.fromJson(json['data']) : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'code': code,
      'msg': msg,
      'data': data?.toJson(),
    };
  }
}

class OrderHomeDetailModel {
  String orderId;

  OrderHomeDetailModel({
    required this.orderId,
  });

  factory OrderHomeDetailModel.fromJson(Map<String, dynamic> json) {
    return OrderHomeDetailModel(
      orderId: json['orderId'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'orderId': orderId,
    };
  }
}
